﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using HYRevitCode.FormUtility.ModelessFormControlSuite.ModelLayer;

namespace SampleAndTest.ModelessFormControlSuite
{
	internal class LineStack : IOperateStack
	{
		internal LineStack(UIDocument inputDoc)
		{
			this.m_useUiDoc = inputDoc;
		}

		public void AddToStack(object input)
		{
			if (input is XYZ && input != null)
			{
				this.m_lstPoits.Add(input as XYZ);
			}
		}

		public void ClearStack()
		{
			this.m_lstPoits = new List<XYZ>();
		}

		public bool TryFinishStack()
		{
			if (this.m_lstPoits.Count >= 2)
			{
				Transaction transaction = new Transaction(this.m_useUiDoc.Document, "addLine");
				try
				{
					Line line = Line.CreateBound(this.m_lstPoits[0], this.m_lstPoits[1]);
					transaction.Start();
					this.m_useUiDoc.Document.Create.NewDetailCurve(this.m_useUiDoc.ActiveView, line);
					transaction.Commit();
				}
				catch (Exception)
				{
					if (transaction.HasStarted())
					{
						transaction.RollBack();
					}
				}
				return true;
			}
			return false;
		}

		private UIDocument m_useUiDoc;

		private List<XYZ> m_lstPoits = new List<XYZ>();
	}
}
